package com.doradance.app.appactivity.search;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.doradance.R;
import com.doradance.account.utils.FragmentUtils;
import com.doradance.account.utils.ListDataSave;
import com.doradance.entity.ShowSearchEntity;
import com.doradance.entity.clearListEntity;
import com.doradance.entity.showCurriumSearchEntity;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.yuyh.library.BubblePopupWindow;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

public class SearchCurriumActivity extends BaseActivity {

    @Bind(R.id.et_search_currium_history)
    EditText mEtSearchCurriumHistory;
    @Bind(R.id.iv_currium_delete_search)
    ImageView mIvCurriumDeleteSearch;
    ListDataSave dataList;
    ArrayList<String> mArrayList = new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        dataList = new ListDataSave(this, "searchhistory");
        Log.e("dfds",dataList.getDataList("searchhistory").size()+"");
        for (int i = 0; i < dataList.getDataList("searchhistory").size(); i++) {
            mArrayList.add(dataList.getDataList("searchhistory").get(i).toString());
        }
        hideToolbar();
        //监听键盘的确定键
        watchSearch();
        FragmentUtils.addFirstFragment(getSupportFragmentManager(), R.id.fl_currium_search, new SearchCurriumHomeFragment());
    }

    private void watchSearch() {
        mEtSearchCurriumHistory.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    // 先隐藏键盘
                    ((InputMethodManager) mEtSearchCurriumHistory.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(SearchCurriumActivity.this
                                            .getCurrentFocus().getWindowToken(),
                                    InputMethodManager.HIDE_NOT_ALWAYS);
                    if (TextUtils.isEmpty(mEtSearchCurriumHistory.getText().toString())) {
                        EasyToast.showToast(getApplicationContext(), "请输入你感兴趣的内容");
                    } else {
                        mArrayList.add(mEtSearchCurriumHistory.getText().toString());
                        dataList.setDataList("searchhistory", mArrayList);
                        SearchVideoFragment searchVideoFragment = new SearchVideoFragment(mEtSearchCurriumHistory.getText().toString());
                        FragmentUtils.replaceFragment(getSupportFragmentManager(), R.id.fl_currium_search, searchVideoFragment);
                        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(SearchCurriumActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                    return true;
                }
                return false;
            }
        });
        mEtSearchCurriumHistory.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                mIvCurriumDeleteSearch.setVisibility(View.VISIBLE);

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(clearListEntity refreshEvent) {
            mArrayList.clear();
            dataList.clearDataList();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(showCurriumSearchEntity refreshEvent) {
        mEtSearchCurriumHistory.setText(refreshEvent.text);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, SearchCurriumActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_search_currium, null);
    }


    @OnClick({R.id.tv_currium_search_cancel,R.id.iv_currium_delete_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_currium_search_cancel:
                finish();
                break;
            case R.id.iv_currium_delete_search:
                mEtSearchCurriumHistory.setText("");
                mIvCurriumDeleteSearch.setVisibility(View.GONE);
                FragmentUtils.replaceFragment(getSupportFragmentManager(), R.id.fl_currium_search, new SearchCurriumHomeFragment());
                break;
        }
    }
}
